#include "com.hpp"

int main()
{
    Init init;
    int shmid = creatshmflg();
    char *shmaddr = (char *)shmat(shmid, nullptr, 0);

    int fd = open(FIFO_FILE, O_RDONLY);
    if (fd == -1)
    {
        perror("open");
        exit(OPEN_ERROR);
    }

    struct shmid_ds shmds;
    while (true)
    {
        char c;
        int n = read(fd, &c, sizeof(c));
        if (n == 0)
        {
            break;
        }
        else if (n < 0)
        {
            break;
        }
        cout << "B received a message from A:" << shmaddr;
        sleep(1);
        
        shmctl(shmid,IPC_STAT,&shmds);
        cout<<"shm size:"<<shmds.shm_segsz<<endl;
        cout<<"shm nattch:"<<shmds.shm_nattch<<endl;
        printf("0x%x\n",shmds.shm_segsz);
        cout<<"shm mode:"<<shmds.shm_perm.mode<<endl;
    }

    shmdt(shmaddr);
    shmctl(shmid, IPC_RMID, nullptr);
    close(fd);
    return 0;
}